package GUI; //done

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import Client.ConnectionToServer;
import Common.Settings;
import Entities.Customer;
import Entities.DealSale;

import javax.swing.JCheckBox;
import javax.swing.JRadioButton;

/**
 * This is the main panel for SalesMan. 
 * @author ameer
 * @author waleed
 * @author bshara
 * @author kamel
 */

public class SalesManMenu extends DealSaleWindow{
	
	/** Customer ID Label */
	private JLabel CusIdLabel = null;
	
	/** Customer Name Label */
	private JLabel CusNameLabel= null;
	
	/** Customer Age Label */
	private JLabel CusAgeLabel= null;
	
	/** Customer Address Zone Label */
	private JLabel CusAddressZoneLabel= null;
	
	/** CustomerPhone Label */
	private JLabel CusPhoneLabel= null;
	
	/** Customer Importance Label */
	private JLabel CusImportance = null;
	
	/** All Customers Label */
	private JLabel customersLabel = null;
	
	/** Label of all rejected deal sales */
	private JLabel rejectedLabel = null;
	
	/** Customer ID text field  */
	private JTextField cIdTextField = null;
	
	/** Customer Name text field  */
	private JTextField cNameTextField = null;
	
	/** Customer Age text field  */
	private JTextField cAgeTextField = null;
	
	/** Customer Phone text field  */
	private JTextField cPhoneTextField= null;
	
	/** Customer Importance text field  */
	private JTextField CusImportanceTextField= null;
	
	/** Customer text field for rejected deal sales  */
	private JTextField rejectedTextField= null;
	
	/** ComboBox for all customers  */
	private JComboBox customerJComboBox= null;
	
	/** ComboBox for all Address Zones  */
	private JComboBox cAddressZoneComboBox= null;
	
	/** Button of rejected Deal Sale */
	private JButton rejectedButton = null;
	
	/** Button of purchased Deal Sale */
	private JButton purchasedButton = null;
	
	/** The Log Out Button */
	private JButton LogOutButton;
	
	/** The user name label. */
	private JLabel usernameLabel;
	
	/** CheckBox for Deny Future Updates */
	private JCheckBox chckbxDenyFutureUpdates;
	
	/** CheckBox for Deny Future Updates */
	private JCheckBox chckbxDenyFutureUpdates2;
	
	/** label for background */
	private JLabel backGroundLabel;
	
	/** aim objects */
	private String username = null;
	private Customer customer = null;
	private DealSale dealsale = null;
	private boolean rejectedFlag;

	
	/**
	 * The default constructor
	 */
	public SalesManMenu(String username) {
		super();
		getBackjButton().setLocation(85, 510);
		this.username = username;
		initialize();		
	}
	
	/**
	 * This function initializes SalesManMenu
	 */
	private void initialize() {
		customer = new Customer(0, "", 0,  0, "");
		dealsale = new DealSale(0, null, null, null,null); 
		rejectedFlag= false;
		
		CusIdLabel = new JLabel();
		CusIdLabel.setBounds(new Rectangle(15, 320, 130, 22));
		CusIdLabel.setText("Customer ID:");
		
		CusNameLabel = new JLabel();
		CusNameLabel.setBounds(new Rectangle(15, 350, 130, 22));
		CusNameLabel.setText("Customer name:");
		
		CusAgeLabel = new JLabel();
		CusAgeLabel.setBounds(new Rectangle(15, 380, 130, 22));
		CusAgeLabel.setText("Customer age:");
		
		CusAddressZoneLabel = new JLabel();
		CusAddressZoneLabel.setBounds(new Rectangle(15, 410, 130, 22));
		CusAddressZoneLabel.setText("Customer area:");
		
		CusPhoneLabel = new JLabel();
		CusPhoneLabel.setBounds(new Rectangle(15,440,130,22));
		CusPhoneLabel.setText("Customer phone");
		
		CusImportance = new JLabel();
		CusImportance.setBounds(new Rectangle(15,470,130,22));
		CusImportance.setText("Customer importance");
		
		customersLabel = new JLabel();
		customersLabel.setBounds(new Rectangle(350, 300, 127, 25));
		customersLabel.setText("All customer:");
		
		rejectedLabel = new JLabel();
		rejectedLabel.setBounds(new Rectangle(350, 356, 127, 25));
		rejectedLabel.setText("Delay message:");
		
		usernameLabel = new JLabel();
		usernameLabel.setText("Welcome " + Settings.Logged_Username);
		usernameLabel.setFont(new Font("Tahoma", Font.BOLD, 13));
		usernameLabel.setBounds(526, 26, 124, 23);
		this.add(usernameLabel, null);
		
		this.add(getLogOutButton());
		this.add(customersLabel, null);
		this.add(rejectedLabel, null);
		this.add(CusIdLabel, null);
		this.add(CusNameLabel, null);
		this.add(CusAgeLabel, null);
		this.add(CusAddressZoneLabel, null);
		this.add(CusPhoneLabel,null);
		this.add(CusImportance,null);
		this.add(getDelayTextField(), null);
		this.add(getCIdTextField(), null);
		this.add(getCNameTextField(), null);
		this.add(getCAageTextField(), null);
		this.add(getCAreaJComboBox(), null);
		this.add(getCPhoneTextField(),null);
		this.add(getCusImportanceTextField(),null);
		this.add(getCustomerJComboBox(), null);	
		this.add(getChckbxDenyFutureUpdates());
		this.add(getChckbxDenyFutureUpdates2());
		this.add(getRejectedButton(), null);
		this.add(getPurchasedButton(), null);		
		
		super.getBackjButton().setBounds(140, 510, 90, 25);	
		super.getBackjButton().setVisible(false);
		
		this.setSize(650, 600);

		JLabel label_1 = new JLabel("");
		label_1.setIcon(new ImageIcon(LoginMenu.class.getResource("/Pictures/Logout-icon.png")));
		label_1.setBounds(526, 26, 124, 146);
		add(label_1);
		
		backGroundLabel = new JLabel("");
		backGroundLabel.setIcon(new ImageIcon(LoginMenu.class.getResource("/Pictures/seller-icon.png")));
		backGroundLabel.setBounds(54, 0, 300, 72);
		add(backGroundLabel);
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(LoginMenu.class.getResource("/Pictures/bg.png")));
		label.setBounds(0, 0, 650, 600);
		add(label);
		
	}
	
	/**
	 * This function initializes LogOutButton	
	 */
	private JButton getLogOutButton() 
	{
		if (LogOutButton == null) 
		{
			LogOutButton = new JButton();
			LogOutButton.setBounds(new Rectangle(537, 80, 103, 29));
			LogOutButton.setText("Log Out");
			LogOutButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{			
					try
					{
						ConnectionToServer connectiontoserver = new ConnectionToServer();
						String loggedofmsg = (String)connectiontoserver.LogOut();
						if(loggedofmsg != null && loggedofmsg.equals("LoggedOut")){
							JOptionPane.showMessageDialog(null, "Logged out successfuly");
							System.exit(0);
						}
						else
							JOptionPane.showMessageDialog(null, "Error!","",JOptionPane.WARNING_MESSAGE);
					} 
					catch (Exception e1){
						System.exit(0);
					}					
				}
			});
		}
		return LogOutButton;
	}
	
	/**
	 * This function initializes rejectedButton
	 */	
	public JButton getRejectedButton(){
		if (rejectedButton == null){
			rejectedButton = new JButton();
			rejectedButton.setText("Rejected");
			rejectedButton.setBounds(new Rectangle(208, 508, 114, 29));
			rejectedButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (rejectedFlag){
						String msg = getDelayTextField().getText();
						if (msg.equals("")){
							JOptionPane.showMessageDialog(null, "Please enter Customer response!","",JOptionPane.WARNING_MESSAGE);
							return;
						}						
						rejectedTextField.setEditable(false);
						getPurchasedButton().setEnabled(true);
						rejectedLabel.setForeground(Color.black);
						rejectedFlag = false;
						long custID = customer.getCustID();
						long DealSaleID = dealsale.getDealSaleID();
						ConnectionToServer connectionToServer;
						try {
							connectionToServer = new ConnectionToServer();
							String serversmsg = (String)connectionToServer.rejectedDealSale(custID,DealSaleID, msg);
							if (serversmsg != null){
								if (serversmsg.equals("OK")){
									JOptionPane.showMessageDialog(null, "Customer response successfuly saved");
									if (customerJComboBox.getItemCount() == 1)
										System.exit(0);
									customerJComboBox.removeItem(custID);
								}
								else
									JOptionPane.showMessageDialog(null, "Customer response not saved","", JOptionPane.WARNING_MESSAGE);
							}	
						} 
						catch (Exception e1){
							JOptionPane.showMessageDialog(null, "Error!","", JOptionPane.ERROR_MESSAGE);
						}					
					}
					else{
						getPurchasedButton().setEnabled(false);
						rejectedTextField.setEditable(true);
						rejectedLabel.setForeground(Color.red);						
						rejectedFlag = true;
					}					
				if(chckbxDenyFutureUpdates.isSelected()){
					ConnectionToServer connectiontoserver = new ConnectionToServer();
					String[] sectionList = (String[]) connectiontoserver.loadAllCategories();
					int CategoryID;
					int len = sectionList.length;
					if(len != 0){
						for(int i = 0; i < len ; i++){
							CategoryID = Integer.parseInt(sectionList[i].substring(0, sectionList[i].indexOf(":")));
							try{
								ConnectionToServer connectiontoserver2 = new ConnectionToServer();
								String msgfromserver = (String)connectiontoserver2.DeleteCustomerInterest(customer.getCustID(), CategoryID);
								if (msgfromserver == null)
									JOptionPane.showMessageDialog(null, "Error, Can't delete interest!");
							}
							catch(Exception e1){
								JOptionPane.showMessageDialog(null, "Error, Can't delete Customer Interests!");
							}
						}
					}
				}
				else
				if(chckbxDenyFutureUpdates2.isSelected()){
					ConnectionToServer connectiontoserver = new ConnectionToServer();
					int[] sectionList = (int[]) connectiontoserver.loadAllCategoriesOfProduct(getProdID());
					int CategoryID;
					int len = sectionList.length;
					if(len != 0){
						for(int i = 0; i < len ; i++){
							CategoryID = sectionList[i];
							try{
								ConnectionToServer connectiontoserver2 = new ConnectionToServer();
								String msgfromserver = (String)connectiontoserver2.DeleteCustomerInterest(customer.getCustID(), CategoryID);
								if (msgfromserver == null)
									JOptionPane.showMessageDialog(null, "Error, Can't delete interest!");
							}
							catch(Exception e1){
								JOptionPane.showMessageDialog(null, "Error, Can't delete Customer Interests!");
							}
						}
					}
				}
			}
			});	
		}	
		return rejectedButton;				
	}
	
	/**
	 * This function initializes Purchased button
	 */	
	public JButton getPurchasedButton(){
		if (purchasedButton == null){
			purchasedButton = new JButton();
			purchasedButton.setText("Purchased");
			purchasedButton.setBounds(new Rectangle(396, 508, 117, 29));
			purchasedButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					long custID = customer.getCustID();
					long DealSaleID = dealsale.getDealSaleID();
					ConnectionToServer connectionToServer;
					try {
						connectionToServer = new ConnectionToServer();
						String serversmsg = (String)connectionToServer.purchasedDealSale(custID,DealSaleID);
						if (serversmsg != null){
							if (serversmsg.equals("OK")){
								JOptionPane.showMessageDialog(null, "Purchase successfuly saved");
								if (customerJComboBox.getItemCount() == 1)
									System.exit(0);
								customerJComboBox.removeItem(custID);
							}
							else
								JOptionPane.showMessageDialog(null, "Purchase not saved","", JOptionPane.WARNING_MESSAGE);
						}	
					} 
					catch (Exception e1){
						JOptionPane.showMessageDialog(null, "ERROR!!","", JOptionPane.ERROR_MESSAGE);
					}
					if(chckbxDenyFutureUpdates.isSelected()){
						ConnectionToServer connectiontoserver = new ConnectionToServer();
						String[] sectionList = (String[]) connectiontoserver.loadAllCategories();
						int CategoryID;
						int len = sectionList.length;
						if(len != 0){
							for(int i = 0; i < len ; i++){
								CategoryID = Integer.parseInt(sectionList[i].substring(0, sectionList[i].indexOf(":")));
								try{
									ConnectionToServer connectiontoserver2 = new ConnectionToServer();
									String msgfromserver = (String)connectiontoserver2.DeleteCustomerInterest(customer.getCustID(), CategoryID);
									if (msgfromserver == null)
										JOptionPane.showMessageDialog(null, "Error, Can't delete interest!");
								}
								catch(Exception e1){
									JOptionPane.showMessageDialog(null, "Error, Can't delete Customer Interests!");
								}
							}
						}
					}
					else
					if(chckbxDenyFutureUpdates2.isSelected()){
						ConnectionToServer connectiontoserver = new ConnectionToServer();
						int[] sectionList = (int[]) connectiontoserver.loadAllCategoriesOfProduct(getProdID());
						int CategoryID;
						int len = sectionList.length;
						if(len != 0){
							for(int i = 0; i < len ; i++){
								CategoryID = sectionList[i];
								try{
									ConnectionToServer connectiontoserver2 = new ConnectionToServer();
									String msgfromserver = (String)connectiontoserver2.DeleteCustomerInterest(customer.getCustID(), CategoryID);
									if (msgfromserver == null)
										JOptionPane.showMessageDialog(null, "Error, Can't delete interest!");
								}
								catch(Exception e1){
									JOptionPane.showMessageDialog(null, "Error, Can't delete Customer Interests!");
								}
							}
						}
					}
				}
			});	
		}	
		return purchasedButton;				
	}
	
	/**
	 * This function initializes cIdTextField	
	 */
	private JTextField getCIdTextField(){
		if (cIdTextField == null) {
			cIdTextField = new JTextField();
			cIdTextField.setBounds(new Rectangle(150, 320, 127, 25));
			cIdTextField.setEditable(false);
		}
		return cIdTextField;
	}
	
	/**
	 * This function initializes DelayTextField	
	 */
	private JTextField getDelayTextField(){
		if (rejectedTextField == null) {
			rejectedTextField = new JTextField();
			rejectedTextField.setBounds(new Rectangle(350, 386, 127, 25));
			rejectedTextField.setEditable(false);
		}
		return rejectedTextField;
	}
	
	/**
	 * This function initializes cNameTextField	
	 */
	private JTextField getCNameTextField(){
		if (cNameTextField == null) {
			cNameTextField = new JTextField();
			cNameTextField.setBounds(new Rectangle(150, 350, 127, 25));
			cNameTextField.setEditable(false);
		}
		return cNameTextField;
	}
	
	/**
	 * This function initializes cAgeTextField	
	 */
	private JTextField getCAageTextField(){
		if (cAgeTextField == null) {
			cAgeTextField = new JTextField();
			cAgeTextField.setBounds(new Rectangle(150, 380, 127, 25));
			cAgeTextField.setEditable(false);
		}
		return cAgeTextField;
	}
	
	/**
	 * This function initializes cAreaTextField	
	 */
	private JComboBox getCAreaJComboBox(){
		if (cAddressZoneComboBox == null) {
			cAddressZoneComboBox = new JComboBox();
			cAddressZoneComboBox.setBounds(new Rectangle(150, 410, 127, 25));
			cAddressZoneComboBox.setEditable(false);
			String[] areaList = null;
			try{
				ConnectionToServer connectiontoserver = new ConnectionToServer();		
				areaList = (String[]) connectiontoserver.loadAllAddressZones();
				if (areaList == null){
					JOptionPane.showMessageDialog(null, "ERROR!!!");
					cAddressZoneComboBox.addItem(" ");
				}	
				else{
					int len = areaList.length;
					if (len == 0)
						cAddressZoneComboBox.addItem("0:EMPTY");
					else
						cAddressZoneComboBox.addItem(" ");
					for(int i=0; i < len; i++){
						cAddressZoneComboBox.addItem(areaList[i]);
					}	
				}
			}
			catch(Exception e){
				JOptionPane.showMessageDialog(null, "ERROR!!!");
				cAddressZoneComboBox.addItem(" ");
			}
			cAddressZoneComboBox.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					
				}	
			});	
		}
		return cAddressZoneComboBox;
	}
	
	/**
	 * This function initializes cPhoneTextField	
	 */
	private JTextField getCPhoneTextField(){
		if (cPhoneTextField == null) {
			cPhoneTextField = new JTextField();
			cPhoneTextField.setBounds(new Rectangle(150, 440, 127, 25));
			cPhoneTextField.setEditable(false);
		}
		return cPhoneTextField;
	}
	
	/**
	 * This function initializes CusImportanceTextField	
	 */
	private JTextField getCusImportanceTextField(){
		if (CusImportanceTextField == null) {
			CusImportanceTextField = new JTextField();
			CusImportanceTextField.setBounds(new Rectangle(150, 470, 127, 25));
			CusImportanceTextField.setEditable(false);	
		}
		return CusImportanceTextField;
	}
	
	/**
	 * This function initializes customerJComboBox	
	 */
	private JComboBox getCustomerJComboBox(){
		if (customerJComboBox == null) {
			customerJComboBox = new JComboBox();
			customerJComboBox.setBounds(new Rectangle(350, 320, 127, 25));
			updateCustomerJComboBox();
			customerJComboBox.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					try {
						ConnectionToServer connectiontoserver = new ConnectionToServer();
						long custID = (Long)customerJComboBox.getSelectedItem();
						long DealSaleID = (Long)connectiontoserver.loadDealSaleForCustomer(custID);
						if (custID > 0 && DealSaleID > 0){
							customer.loadCustomer(custID);
							dealsale.loadDealSale(DealSaleID);
							loadForm();
						}						
					} 
					catch (Exception e1) {
						
					}	
				}	
			});	
		}
		return customerJComboBox;
	}
	
	/**
	  * This function updates customerJComboBox
	 */
	public void updateCustomerJComboBox(){
		long[] CustomersList = null;
		try{
			ConnectionToServer connectiontoserver = new ConnectionToServer();		
			CustomersList = (long[])connectiontoserver.loadAllCustomersForSalesMan(username);
			if (CustomersList == null){
				JOptionPane.showMessageDialog(null, "Error!");
				customerJComboBox.addItem(' ');
			}	
			else{
				int len = CustomersList.length;
				if (len == 0)
					customerJComboBox.addItem('0');	
				else
					customerJComboBox.addItem(' ');
				for(int i=0; i < len; i++){
					customerJComboBox.addItem(CustomersList[i]);
				}	
			}
		}
		catch(Exception e){
			JOptionPane.showMessageDialog(null, "Error!");
			customerJComboBox.addItem(' ');
		}		
	}
	
	/**
	 * This function load client list to form
	 */
	public void loadForm(){
		super.loadDealSale(dealsale);
		cIdTextField.setText(String.valueOf(customer.getCustID()));
		cNameTextField.setText(customer.getCustName()) ;
		cAgeTextField.setText(String.valueOf(customer.getCustAge()));
		cAddressZoneComboBox.setSelectedItem(customer.getCustAddressZone());
		cPhoneTextField.setText(customer.getCustPhone()) ;
		CusImportanceTextField.setText(String.valueOf(customer.getImportance()));	
	}
	private JCheckBox getChckbxDenyFutureUpdates() {
		if (chckbxDenyFutureUpdates == null) {
			chckbxDenyFutureUpdates = new JCheckBox("Deny future updates for all Categories");
			chckbxDenyFutureUpdates.setBounds(350, 468, 266, 23);
		}
		return chckbxDenyFutureUpdates;
	}
	
	private JCheckBox getChckbxDenyFutureUpdates2() {
		if (chckbxDenyFutureUpdates2 == null) {
			chckbxDenyFutureUpdates2 = new JCheckBox("Deny future updates for this Category");
			chckbxDenyFutureUpdates2.setBounds(350, 440, 266, 23);
		}
		return chckbxDenyFutureUpdates2;
	}
}
